angular.module('app').controller('registerCtrl', ['$http', '$scope', 'dict', '$interval', '$state', function ($http, $scope, dict, $interval, $state) {
    $scope.sendInfo = "发送信息";
    $scope.issend = false;
    var sendTimer = null;
    var sendtime = 0;
    $scope.send = function(){
        if(!$scope.issend){
            $scope.issend = true;
            ajax_send();
        }
    }
    $scope.submit = function () {
        $http({
            url: "data/regist.json",
            method: "GET",
            data : $scope.user
        }).then(function(res){
            console.log(res);
            $state.go('login');
        }, function(error){
            console.log(error);
        });
    }
    function ajax_send(){
        $http({
            url: "data/code.json",
            method: 'GET',
        }).then(function(res){
            console.log(res);
            if(res.data.state == 1){
                sendtime = 5;
                $scope.sendTime = "5s";
                $scope.sendInfo = "发送中("+ $scope.sendTime +")";
                sendTimer = $interval(function(){
                    if(sendtime <=0){
                        $interval.cancel(sendTimer);
                        $scope.issend = false;
                        $scope.sendInfo = "重新发送";
                        return;
                    }
                    sendtime -= 1;
                    $scope.sendTime = sendtime+"s";
                    $scope.sendInfo = "发送中("+ $scope.sendTime +")";
                },1000);
            }
        },function(error){
            console.log(error);
        });
    }

}]);

